/**
 * [一句话描述该类的功能]
 *
 * @author : [kai]
 * @version : [v1.0]
 * @createTime : [2022/10/20 19:53]
 */


package com.find.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.find.controller.utils.R;
import com.find.domain.User;
import com.find.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@Api(tags="用户相关操作")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("获取所有用户信息")
    @GetMapping
    public R getAll(){
        return new R(true, userService.list());
    }

    @ApiOperation("添加用户")
    @ApiImplicitParam(name = "user",value = "用户类",required = true)
    @ApiResponses({
            @ApiResponse(code = 200,message = "添加成功"),
            @ApiResponse(code = 500,message = "添加失败")
    })
    @PostMapping
    public R save(@RequestBody User user, HttpServletResponse response) throws IOException {
        boolean flag = userService.saveUser(user);
        response.setStatus(flag?200:500);
        return new R(flag, flag ? "添加成功" : "添加失败!");
    }

    @PutMapping
    public R update(@RequestBody Map<String,Object> user,HttpServletRequest request,HttpServletResponse response) throws IOException {
        Cookie[] cookies=request.getCookies();
        String name="";
        System.out.println(user.toString());
        System.out.println(name);
        String newName = userService.updateByName(user,user.get("oldname").toString());
        if(newName==null){
            response.setStatus(500);
            return new R(false,"修改失败");
        }
        response.setHeader("Cookie",newName);
        response.setStatus(200);
        User nowUser=userService.getByName(newName);
        return new R(true, nowUser);
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id,HttpServletResponse response){
        boolean flag=userService.delete(id);
        response.setStatus(flag?200:500);
        return new R(flag,flag?"删除成功":"删除失败");
    }

    @GetMapping("/select")
    public R getById(@RequestParam Map<String, Object> map,HttpServletResponse response){
        response.setStatus(200);
        return new R(true, userService.listByMap(map));
    }


    @GetMapping("{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,HttpServletResponse response) {
        IPage<User> page = userService.getPage(currentPage, pageSize);
        if (currentPage > page.getPages()) {
            page = userService.getPage((int) page.getPages(), pageSize);
        }
        response.setStatus(200);
        return new R(true, page);
    }
}
